const util = require('../../utils/util.js');
const api = require('../../utils/api.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        images_list: [],
        image_address: [],
        page:1,
        pageSize:10,
    },
    //touch start
    handleTouchStart: function (e) {
        this.startTime = e.timeStamp;
    },
    handleTouchEnd: function (e) {
        this.endTime = e.timeStamp;
    },
    //长按点击
    handleLongPress: function (e) {
        var that = this
        wx.showModal({
            title: '提示',
            content: '确定要删除此图片吗？',
            success: function (res) {
                if (res.confirm) {
                    var that1 = that
                    util.request(api.ApiDelUploadImage, {image_id:e.currentTarget.dataset["index"],address:e.currentTarget.dataset["address"]}).then(function (res) {
                        if (res.code == 200){
                            wx.showToast({
                                title: '删除成功',
                                icon: 'success',
                                duration: 2000
                            })
                            that1.getUploadImage()
                        }else{
                            wx.showToast({
                                title: '删除失败',
                                icon: 'error',
                                duration: 2000
                            })
                        }
                    })
                  }
            }
        })
    },
    //展示图片
    showImage(e) {
        wx.previewImage({
            current: e.currentTarget.dataset["address"], // 当前显示图片的http链接
            urls: this.data.image_address // 需要预览的图片http链接列表
        })
    },
    //查询图片
    getUploadImage() {
        var params = {
            openid: wx.getStorageSync('openid'),
            types: 0,
            page:this.data.page,
            pageSize:this.data.pageSize
        }
        let that = this
        util.request(api.ApiGetUploadImage, params).then(function (res) {
            let list = []
            res.data.forEach(item => {
                list.push(item.address)
            });
            var datas = that.data.images_list
            var lists = that.data.image_address
            datas = that.data.images_list.concat(res.data)
            lists = that.data.image_address.concat(list)
            that.setData({
                images_list: datas,
                image_address: lists
            })
        })
    },
    //上传
    doUpload() {
        let that = this
        wx.chooseImage({
            success(res) {
                const tempFilePaths = res.tempFilePaths
                let that1 = that
                if (tempFilePaths.length >1) {
                    wx.showToast({
                        title: '选择单张',
                        icon: 'error',
                        duration: 2000
                    })
                    return
                }
                wx.uploadFile({
                    url: api.ApiUploadImage, //仅为示例，非真实的接口地址
                    filePath: tempFilePaths[0],
                    name: 'file',
                    formData: {
                        openid: wx.getStorageSync('openid'),
                        types: 0,
                    },
                    success(res) {
                        var data = JSON.parse(res.data)
                        if (data.code == 200) {
                            that1.getUploadImage()
                            wx.showToast({
                                title: '上传成功',
                                icon: 'success',
                                duration: 2000
                            })
                        } else {
                            wx.showToast({
                                title: '上传失败',
                                icon: 'error',
                                duration: 2000
                            })
                        }
                    }
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getUploadImage()
    },



    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        console.log(1211)
        this.setData({
            page:1,
            images_list: [],
            image_address: [],
        })
        this.getUploadImage()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.setData({
            page:this.data.page  + 1
        })
        this.getUploadImage()
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})